using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont font;

        enum ComboType
        {
            Light,
            Heavy
        }

        uint nivel, indexElemento, indexArma, bastaoBase, marteloBase, katanaBase, desarmadoBase, fullbladeBase, foiceBase, escudoBase, espadasBase;
        double forca, destreza, constituicao, balance, clickDelay, danoCombo;
        Texture2D point, selection, square;
        Vector2 selectionElement, selectionWeapon, firePosition, waterPosition, earthPosition, windPosition, maisPosition, menosPosition, resetPosition, marteloPosition,
            katanaPosition, desarmadoPosition, bastaoPosition, fullbladePosition, foicePosition, escudoPosition, espadasPosition, fracoPosition, fortePosition;
        MouseState mouseState;
        bool mousePressed;
        List<ComboType> combos;
        float condicional;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            forca = destreza = constituicao = balance = 10.0f;
            firePosition = new Vector2(765, 20);
            waterPosition = new Vector2(765, 60);
            earthPosition = new Vector2(765, 100);
            windPosition = new Vector2(765, 140);
            maisPosition = new Vector2(110, 8);
            menosPosition = new Vector2(140, 8);
            resetPosition = new Vector2(770, 450);
            marteloPosition = new Vector2(10, 349);
            espadasPosition = new Vector2(10, 424);
            bastaoPosition = new Vector2(10, 274);
            katanaPosition = new Vector2(10, 374);
            fullbladePosition = new Vector2(10, 399);
            foicePosition = new Vector2(10, 449);
            escudoPosition = new Vector2(10, 299);
            desarmadoPosition = new Vector2(10, 324);
            fracoPosition = new Vector2(320, 50);
            fortePosition = new Vector2(350, 50);
            selectionWeapon = bastaoPosition - new Vector2(2);
            combos = new List<ComboType>();
            nivel = 0;
            danoCombo = 0;
            condicional = 0;
            indexElemento = 1;
            indexArma = 1;
            calculaBase();
            this.IsMouseVisible = true;
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            font = Content.Load<SpriteFont>("font");
            point = Content.Load<Texture2D>("point");
            selection = Content.Load<Texture2D>("selection");
            square = Content.Load<Texture2D>("square");
        }

        protected override void UnloadContent()
        {
            
        }

        protected override void Update(GameTime gameTime)
        {
            mouseState = Mouse.GetState();

            switch (indexElemento)
            {
                case 1:
                    selectionElement = firePosition - new Vector2(2);
                    break;
                case 2:
                    selectionElement = waterPosition - new Vector2(2);
                    break;
                case 3:
                    selectionElement = earthPosition - new Vector2(2);
                    break;
                case 4:
                    selectionElement = windPosition - new Vector2(2);
                    break;
            }

            switch (indexArma)
            {
                case 1:
                    selectionWeapon = bastaoPosition - new Vector2(2);
                    break;
                case 2:
                    selectionWeapon = escudoPosition - new Vector2(2);
                    break;
                case 3:
                    selectionWeapon = desarmadoPosition - new Vector2(2);
                    break;
                case 4:
                    selectionWeapon = marteloPosition - new Vector2(2);
                    break;
                case 5:
                    selectionWeapon = katanaPosition - new Vector2(2);
                    break;
                case 6:
                    selectionWeapon = fullbladePosition - new Vector2(2);
                    break;
                case 7:
                    selectionWeapon = espadasPosition - new Vector2(2);
                    break;
                case 8:
                    selectionWeapon = foicePosition - new Vector2(2);
                    break;
            }

            clickDelay -= gameTime.ElapsedGameTime.TotalMilliseconds;
            if (clickDelay < 0)
            {
                mousePressed = false;
            }

            if (mouseState.LeftButton == ButtonState.Pressed && !mousePressed)
            {
                mousePressed = true;
                clickDelay = 100;
                checkMouse(mouseState.X, mouseState.Y);
                calculaBase();
            }

            base.Update(gameTime);
        }

        //precisa nova formula
        private void calculaCombo()
        {
            Random rnd = new Random();
            danoCombo = 0;
            switch (indexArma)
            {
                case 1:
                    for (int i = 0; i < combos.Count; i++)
                    {
                        float x = (bastaoBase - nivel) * 2 / (rnd.Next(3) + 8);
                        if (combos[i] == ComboType.Heavy)
                        {
                            danoCombo += bastaoBase + rnd.Next((int)x);
                        }
                        else
                        {
                            danoCombo += bastaoBase - rnd.Next((int)x);
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < combos.Count; i++)
                    {
                        float x = (escudoBase - nivel) * 2 / (rnd.Next(3) + 8);
                        if (combos[i] == ComboType.Heavy)
                        {
                            danoCombo += escudoBase + rnd.Next((int)x);
                        }
                        else
                        {
                            danoCombo += escudoBase - rnd.Next((int)x);
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < combos.Count; i++)
                    {
                        float x = (desarmadoBase - nivel) * 2 / (rnd.Next(3) + 8);
                        if (combos[i] == ComboType.Heavy)
                        {
                            danoCombo += desarmadoBase + rnd.Next((int)x);
                        }
                        else
                        {
                            danoCombo += desarmadoBase - rnd.Next((int)x);
                        }
                    }
                    break;
                case 4:
                    for (int i = 0; i < combos.Count; i++)
                    {
                        float x = (marteloBase - nivel) * 2 / (rnd.Next(3) + 8);
                        if (combos[i] == ComboType.Heavy)
                        {
                            danoCombo += marteloBase + rnd.Next((int)x);
                        }
                        else
                        {
                            danoCombo += marteloBase - rnd.Next((int)x);
                        }
                    }
                    break;
                case 5:
                    for (int i = 0; i < combos.Count; i++)
                    {
                        float x = (katanaBase - nivel) * 2 / (rnd.Next(3) + 8);
                        if (combos[i] == ComboType.Heavy)
                        {
                            danoCombo += katanaBase + rnd.Next((int)x);
                        }
                        else
                        {
                            danoCombo += katanaBase - rnd.Next((int)x);
                        }
                    }
                    break;
                case 6:
                    for (int i = 0; i < combos.Count; i++)
                    {
                        float x = (fullbladeBase - nivel) * 2 / (rnd.Next(3) + 8);
                        if (combos[i] == ComboType.Heavy)
                        {
                            danoCombo += fullbladeBase + rnd.Next((int)x);
                        }
                        else
                        {
                            danoCombo += fullbladeBase - rnd.Next((int)x);
                        }
                    }
                    break;
                case 7:
                    for (int i = 0; i < combos.Count; i++)
                    {
                        float x = (espadasBase - nivel) * 2 / (rnd.Next(3) + 8);
                        if (combos[i] == ComboType.Heavy)
                        {
                            danoCombo += espadasBase + rnd.Next((int)x);
                        }
                        else
                        {
                            danoCombo += espadasBase - rnd.Next((int)x);
                        }
                    }
                    break;
                case 8:
                    for (int i = 0; i < combos.Count; i++)
                    {
                        float x = (foiceBase - nivel) * 2 / (rnd.Next(3) + 8);
                        if (combos[i] == ComboType.Heavy)
                        {
                            danoCombo += foiceBase + rnd.Next((int)x);
                        }
                        else
                        {
                            danoCombo += foiceBase - rnd.Next((int)x);
                        }
                    }
                    break;
            }
        }

        private void checkMouse(int x, int y)
        {
            //troca elemento
            if (x > firePosition.X && x < firePosition.X + point.Width &&
                y > firePosition.Y && y < firePosition.Y + point.Height)
            {
                indexElemento = 1;
                return;
            }
            else if (x > waterPosition.X && x < waterPosition.X + point.Width &&
               y > waterPosition.Y && y < waterPosition.Y + point.Height)
            {
                indexElemento = 2;
                return;
            }
            else if (x > earthPosition.X && x < earthPosition.X + point.Width &&
               y > earthPosition.Y && y < earthPosition.Y + point.Height)
            {
                indexElemento = 3;
                return;
            }
            else if (x > windPosition.X && x < windPosition.X + point.Width &&
               y > windPosition.Y && y < windPosition.Y + point.Height)
            {
                indexElemento = 4;
                return;
            }

            //add
            if (x > maisPosition.X && x < maisPosition.X + point.Width &&
                y > maisPosition.Y && y < maisPosition.Y + point.Height &&
                nivel < 60 && mousePressed)
            {
                nivel++;
                addStats();
                return;
            }

            //remove
            if (x > menosPosition.X && x < menosPosition.X + point.Width &&
                y > menosPosition.Y && y < menosPosition.Y + point.Height &&
                nivel > 0 && mousePressed)
            {
                nivel--;
                removeStats();
                return;
            }

            //reset
            if (x > resetPosition.X && x < resetPosition.X + point.Width &&
                y > resetPosition.Y && y < resetPosition.Y + point.Height &&
                nivel > 0 && mousePressed)
            {
                nivel = 0;
                resetStats();
                return;
            }

            //troca arma
            if (x > bastaoPosition.X && x < bastaoPosition.X + point.Width &&
                y > bastaoPosition.Y && y < bastaoPosition.Y + point.Height)
            {
                indexArma = 1;
                return;
            }
            else if (x > escudoPosition.X && x < escudoPosition.X + point.Width &&
               y > escudoPosition.Y && y < escudoPosition.Y + point.Height)
            {
                indexArma = 2;
                return;
            }
            else if (x > desarmadoPosition.X && x < desarmadoPosition.X + point.Width &&
               y > desarmadoPosition.Y && y < desarmadoPosition.Y + point.Height)
            {
                indexArma = 3;
                return;
            }
            else if (x > marteloPosition.X && x < marteloPosition.X + point.Width &&
               y > marteloPosition.Y && y < marteloPosition.Y + point.Height)
            {
                indexArma = 4;
                return;
            }
            else if (x > katanaPosition.X && x < katanaPosition.X + point.Width &&
               y > katanaPosition.Y && y < katanaPosition.Y + point.Height)
            {
                indexArma = 5;
                return;
            }
            else if (x > fullbladePosition.X && x < fullbladePosition.X + point.Width &&
               y > fullbladePosition.Y && y < fullbladePosition.Y + point.Height)
            {
                indexArma = 6;
                return;
            }
            else if (x > espadasPosition.X && x < espadasPosition.X + point.Width &&
               y > espadasPosition.Y && y < espadasPosition.Y + point.Height)
            {
                indexArma = 7;
                return;
            }
            else if (x > foicePosition.X && x < foicePosition.X + point.Width &&
               y > foicePosition.Y && y < foicePosition.Y + point.Height)
            {
                indexArma = 8;
                return;
            }

            //combo
            if (x > fracoPosition.X && x < fracoPosition.X + point.Width &&
                y > fracoPosition.Y && y < fracoPosition.Y + point.Height)
            {
                if (combos.Count < 22)
                {
                    combos.Add(ComboType.Light);
                    calculaCombo();
                }
                return;
            }
            else if (x > fortePosition.X && x < fortePosition.X + point.Width &&
               y > fortePosition.Y && y < fortePosition.Y + point.Height)
            {
                if(combos.Count < 22)
                {
                    combos.Add(ComboType.Heavy);
                    calculaCombo();
                }
                return;
            }
        }

        #region nao toque

        private void resetStats()
        {
            forca = 10;
            destreza = 10;
            constituicao = 10;
            balance = 10;
        }

        private void removeStats()
        {
            switch (indexElemento)
            {
                case 1:
                    forca -= 4;
                    destreza -= 1.4;
                    constituicao -= 1.4;
                    balance -= 0.6;
                    break;
                case 2:
                    forca -= 0.6;
                    destreza -= 1.4;
                    constituicao -= 1.4;
                    balance -= 4;
                    break;
                case 3:
                    forca -= 1.4;
                    destreza -= 0.6;
                    constituicao -= 4;
                    balance -= 1.4;
                    break;
                case 4:
                    forca -= 1.4;
                    destreza -= 4;
                    constituicao -= 0.6;
                    balance -= 1.4;
                    break;
            }
        }

        private void addStats()
        {
            switch (indexElemento)
            {
                case 1:
                    forca += 4;
                    destreza += 1.4;
                    constituicao += 1.4;
                    balance += 0.6;
                    break;
                case 2:
                    forca += 0.6;
                    destreza += 1.4;
                    constituicao += 1.4;
                    balance += 4;
                    break;
                case 3:
                    forca += 1.4;
                    destreza += 0.6;
                    constituicao += 4;
                    balance += 1.4;
                    break;
                case 4:
                    forca += 1.4;
                    destreza += 4;
                    constituicao += 0.6;
                    balance += 1.4;
                    break;
            }
        }

        private void calculaBase()
        {
            switch (indexElemento)
            {
                case 1:
                    fullbladeBase = (uint)(forca * 1.4);
                    foiceBase = (uint)(forca * 1.3);
                    marteloBase = foiceBase;
                    katanaBase = (uint)(forca * 1.1);
                    desarmadoBase = katanaBase;
                    bastaoBase = (uint)forca;
                    espadasBase = (uint)forca;
                    escudoBase = (uint)(forca * 0.8);
                    break;
                case 2:
                    fullbladeBase = (uint)(balance * 1.4);
                    foiceBase = (uint)(balance * 1.3);
                    marteloBase = foiceBase;
                    katanaBase = (uint)(balance * 1.1);
                    desarmadoBase = katanaBase;
                    bastaoBase = (uint)balance;
                    espadasBase = (uint)balance;
                    escudoBase = (uint)(balance * 0.8);
                    break;
                case 3:
                    fullbladeBase = (uint)(constituicao * 1.4);
                    foiceBase = (uint)(constituicao * 1.3);
                    marteloBase = foiceBase;
                    katanaBase = (uint)(constituicao * 1.1);
                    desarmadoBase = katanaBase;
                    bastaoBase = (uint)constituicao;
                    espadasBase = (uint)constituicao;
                    escudoBase = (uint)(constituicao * 0.8);
                    break;
                case 4:
                    fullbladeBase = (uint)(destreza * 1.4);
                    foiceBase = (uint)(destreza * 1.3);
                    marteloBase = foiceBase;
                    katanaBase = (uint)(destreza * 1.1);
                    desarmadoBase = katanaBase;
                    bastaoBase = (uint)destreza;
                    espadasBase = (uint)destreza;
                    escudoBase = (uint)(destreza * 0.8);
                    break;
            }
        }

        #endregion

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            spriteBatch.Begin();

            //nivel
            spriteBatch.DrawString(font, "Nivel: " + nivel, new Vector2(10, 10), Color.Black);
            spriteBatch.Draw(point, maisPosition, Color.LightGray);
            spriteBatch.Draw(point, menosPosition, Color.LightGray);
            spriteBatch.DrawString(font, "+", maisPosition - new Vector2(-4, 4), Color.Black);
            spriteBatch.DrawString(font, "-", menosPosition - new Vector2(-4, 4), Color.Black);

            //atributos
            spriteBatch.DrawString(font, "Forca: " + forca, new Vector2(10, 50), Color.Black);
            spriteBatch.DrawString(font, "Destreza: " + destreza, new Vector2(10, 70), Color.Black);
            spriteBatch.DrawString(font, "Constituicao: " + constituicao, new Vector2(10, 90), Color.Black);
            spriteBatch.DrawString(font, "Balance: " + balance, new Vector2(10, 110), Color.Black);

            //elementos
            spriteBatch.Draw(selection, selectionElement, Color.Black);
            spriteBatch.Draw(point, firePosition, Color.Red);
            spriteBatch.Draw(point, waterPosition, Color.Blue);
            spriteBatch.Draw(point, earthPosition, Color.Green);
            spriteBatch.Draw(point, windPosition, Color.Yellow);

            //reset
            spriteBatch.Draw(selection, resetPosition, Color.LightGray);
            spriteBatch.DrawString(font, "R", resetPosition - new Vector2(-6, 2), Color.Black);

            //armas
            spriteBatch.DrawString(font, "Foice", new Vector2(40, 445), Color.Black);
            spriteBatch.DrawString(font, "Espadas Duplas", new Vector2(40, 420), Color.Black);
            spriteBatch.DrawString(font, "Fullblade", new Vector2(40, 395), Color.Black);
            spriteBatch.DrawString(font, "Katana(Iaijutsu)", new Vector2(40, 370), Color.Black);
            spriteBatch.DrawString(font, "Martelo", new Vector2(40, 345), Color.Black);
            spriteBatch.DrawString(font, "Desarmado", new Vector2(40, 320), Color.Black);
            spriteBatch.DrawString(font, "Escudo", new Vector2(40, 295), Color.Black);
            spriteBatch.DrawString(font, "Bastao", new Vector2(40, 270), Color.Black);
            spriteBatch.Draw(selection, selectionWeapon, Color.Black);
            spriteBatch.Draw(point, bastaoPosition, Color.LightGray);
            spriteBatch.Draw(point, katanaPosition, Color.LightGray);
            spriteBatch.Draw(point, espadasPosition, Color.LightGray);
            spriteBatch.Draw(point, marteloPosition, Color.LightGray);
            spriteBatch.Draw(point, desarmadoPosition, Color.LightGray);
            spriteBatch.Draw(point, foicePosition, Color.LightGray);
            spriteBatch.Draw(point, fullbladePosition, Color.LightGray);
            spriteBatch.Draw(point, escudoPosition, Color.LightGray);

            //Dano Base
            spriteBatch.DrawString(font, ""+foiceBase, new Vector2(250, 445), Color.Black);
            spriteBatch.DrawString(font, ""+espadasBase, new Vector2(250, 420), Color.Black);
            spriteBatch.DrawString(font, ""+fullbladeBase, new Vector2(250, 395), Color.Black);
            spriteBatch.DrawString(font, ""+katanaBase, new Vector2(250, 370), Color.Black);
            spriteBatch.DrawString(font, ""+marteloBase, new Vector2(250, 345), Color.Black);
            spriteBatch.DrawString(font, ""+desarmadoBase, new Vector2(250, 320), Color.Black);
            spriteBatch.DrawString(font, ""+escudoBase, new Vector2(250, 295), Color.Black);
            spriteBatch.DrawString(font, ""+bastaoBase, new Vector2(250, 270), Color.Black);

            //Dano Combo
            spriteBatch.Draw(square, new Rectangle(310, 40, 350, 175), Color.Black);
            spriteBatch.Draw(square, new Rectangle(312, 42, 346, 171), Color.White);
            spriteBatch.Draw(square, new Rectangle(310, 80, 350, 135), Color.Black);
            spriteBatch.Draw(square, new Rectangle(312, 82, 346, 131), Color.White);
            spriteBatch.Draw(square, new Rectangle(310, 170, 350, 45), Color.Black);
            spriteBatch.Draw(square, new Rectangle(312, 172, 346, 41), Color.White);
            spriteBatch.DrawString(font, "Dano: " + danoCombo, new Vector2(430, 180), Color.Black);
            spriteBatch.Draw(square, fracoPosition, Color.LightGray);
            spriteBatch.Draw(square, fortePosition, Color.LightGray);
            spriteBatch.DrawString(font, "L", fracoPosition - new Vector2(-6, 2), Color.Black);
            spriteBatch.DrawString(font, "H", fortePosition - new Vector2(-6, 2), Color.Black);

            for (int i = 0; i < combos.Count; i++)
            {
                if (i < 11)
                {
                    spriteBatch.Draw(square, new Vector2(323 + (i * 30), 97), Color.LightGray);
                    if(combos[i] == ComboType.Heavy)
                        spriteBatch.DrawString(font, "H", new Vector2(329 + (i * 30), 95), Color.Black);
                    else
                        spriteBatch.DrawString(font, "L", new Vector2(329 + (i * 30), 95), Color.Black);
                }
                else
                {
                    spriteBatch.Draw(square, new Vector2(323 + ((i - 11) * 30), 127), Color.LightGray);
                    if (combos[i] == ComboType.Heavy)
                        spriteBatch.DrawString(font, "H", new Vector2(329 + ((i - 11) * 30), 125), Color.Black);
                    else
                        spriteBatch.DrawString(font, "L", new Vector2(329 + ((i - 11) * 30), 125), Color.Black);
                }
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
